﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Billing.WindowsBase.CustomControls
{
    [ToolboxItem(true)]
    [DesignerAttribute(typeof(sbTextBoxDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    [DefaultEvent("Initialized")]
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    public partial class sbCheckBox : BaseControl
    {
        public sbCheckBox()
        {
            InitializeComponent();
        }

        public event InitializedHandler Initialized;

        public event KeyEventHandler TextBoxKeyUp;
        public override event EventHandler ControlValueChanged;

        public void Initialize()
        {
            if (this.Initialized != null)
                this.Initialized(this);
        }

        #region Public Properties

        private IBaseObject baseObject;
        public IBaseObject BaseObject
        {
            get { return baseObject; }
            set
            {
                baseObject = value;
                if (baseObject == null)
                { this.chk.Text = string.Empty; }
                else
                { this.chk.Text = value.Name; }
            }
        }
        #endregion

        private void txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.TextBoxKeyUp != null)
                this.TextBoxKeyUp(sender, e);
        }



        #region Label
        [Category("Custom")]
        public override string LabelText
        {
            get { return this.lblName.Text; }
            set
            {
                this.lblName.Text = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }

        private void AutoSizeLabel()
        {
            System.Drawing.Graphics gr = this.CreateGraphics();
            this.lblName.Size = new Size((int)gr.MeasureString(this.lblName.Text, this.lblName.Font).Width + 8, this.lblName.Height);
        }
        [Category("Custom")]
        public override bool LabelAutoSize
        {
            get
            {
                return base.LabelAutoSize;
            }
            set
            {
                base.LabelAutoSize = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }
        [Category("Custom")]
        public override Size LabelSize
        {
            get { return this.lblName.Size; }
            set { this.lblName.Size = value; }
        }
        [Category("Custom")]
        public override bool LabelVisible
        {
            get { return this.lblName.Visible; }
            set { this.lblName.Visible = value; }
        }

        public override Color LabelBorderColor
        {
            get { return this.lblName.BackColor; }
            set { this.lblName.BackColor = value; }
        }
        #endregion

        #region Editor
        [Category("Custom")]
        public override string EditorText
        {
            get { return this.chk.Text; }
            set { this.chk.Text = value; }
        }
        [Category("Custom")]
        public override Size EditorSize
        {
            get { return this.chk.Size; }
            set { this.chk.Size = value; }
        }
        [Category("Custom")]
        public override bool EditorVisible
        {
            get { return this.chk.Visible; }
            set { this.chk.Visible = value; }
        }
        [Category("Custom")]
        public override object EditorSource
        {
            get { return null; }
            set { ;}
        }
        [Category("Custom")]
        public override object EditorValue
        {
            get { return this.chk.Checked; }
            set { this.chk.Checked = (bool)value; }
        }
        [Category("Custom")]
        public override bool EditorIsReadOnly
        {
            get { return !this.chk.Enabled; }
            set { this.chk.Enabled = !value; }
        }
        #endregion

        private void chk_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ControlValueChanged != null)
                this.ControlValueChanged(sender, e);
        }
    }

    #region "Designer
    public class sbCheckBoxDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public override System.Windows.Forms.Design.SelectionRules SelectionRules
        {
            get
            {
                System.Windows.Forms.Design.SelectionRules i = System.Windows.Forms.Design.SelectionRules.RightSizeable |
                    System.Windows.Forms.Design.SelectionRules.Visible |
                    System.Windows.Forms.Design.SelectionRules.LeftSizeable |
                    System.Windows.Forms.Design.SelectionRules.Moveable;

                return i;
            }
        }
    }
    #endregion
}
